<?php
namespace app\overlord\model;

use app\BaseModel;
use app\member\model\Member;
use app\member\model\StoredOrder;
use app\restaurant\model\Store;
use think\facade\Db;

class Record extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_overlord_atv_record';



    protected $append = [

        'atv_info',

        'content',

        'store_name'
    ];


    /**
     * @author chenniang
     * @DataTime: 2020-11-27 14:14
     * @功能说明:门店名称
     */
    public function getStoreNameAttr($value,$data){

        if(!empty($data['store_id'])){

            $store_name = new Store();

            $name = $store_name->where(['id'=>$data['store_id']])->value('title');

            return $name;

        }

    }


    /**
     * @param $value
     * @param $data
     * @功能说明:内容
     * @author chenniang
     * @DataTime: 2020-11-26 17:37
     */
    public function getContentAttr($value,$data){

        if(!empty($data['id'])){

            $arr = Db::name('longbing_card_overlord_content_record')->where(['record_id'=>$data['id'],'status'=>1])->select()->toArray();

            $content_model = new Content();

            if(!empty($arr)){

                foreach ($arr as $k => $v){

                    if($v['type']==3){

                        $text = explode(',',$v['text']);

                        $arr[$k]['text'] = $text;

                    }

                    $arr[$k]['title']= $content_model->where(['id'=>$v['content_id']])->value('title');
                }
            }

            return $arr;

        }

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-26 11:06
     * @功能说明:活动详情
     */
    public function getAtvInfoAttr($value,$data){


        if(!empty($data['atv_id'])){

            $atv_model = new Active();

            $info = $atv_model->dataInfo(['id'=>$data['atv_id']]);

            return $info;

        }

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:内容添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        $data['status']      = 1;

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:内容编辑
     */
    public function dataUpdate($dis,$data){

        $data['update_time'] = time();

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis){

       $data = $this->alias('a')
               ->join('longbing_card_user b','a.user_id = b.id','left')
               ->where($dis)
               ->field('b.nickName,b.avatarUrl')
               ->group('a.id')
               ->order('a.id desc')
               ->select()
               ->toArray();

       return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-26 10:46
     * @功能说明:报名数量
     */
    public function recordCount($atv_id,$store_id = '',$user_id = ''){

        $where[] = ['atv_id','=',$atv_id];

        if(!empty($store_id)){

            $where[] = ['store_id','=',$store_id];
        }
        //除开自己
        if(!empty($user_id)){

            $where[] = ['user_id','<>',$user_id];

        }

        $where[] = ['status','>',-1];

        $count = $this->where($where)->count();

        return $count;

    }

    /**
     * @author chenniang
     * @DataTime: 2020-11-26 11:12
     * @功能说明:详情
     */
    public function recordInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];


    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-26 11:26
     * @功能说明:记录列表
     */
    public function recordList($dis,$page = 10){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

        return $data;

    }





    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function userRecordList($dis,$page=10){

        $data = $this->alias('a')
            ->join('longbing_card_user b','a.user_id = b.id','left')
            ->join('longbing_card_restaurant_store c','a.store_id = c.id','left')
            ->where($dis)
            ->field('b.nickName,b.avatarUrl,a.*')
            ->group('a.id')
            ->paginate($page)
            ->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-26 17:49
     * @功能说明:已经领取的钱
     */
    public function storeRecordFee($store_id,$atv_id=''){

        $dis = [

            'status'   => 3,

            'store_id' => $store_id
        ];

        if(!empty($atv_id)){

            $dis['atv_id'] = $atv_id;
        }

        $cash = $this->where($dis)->sum('cash');

        return round($cash,2);


    }

    /**
     * @author chenniang
     * @DataTime: 2020-11-26 17:49
     * @功能说明:已经领取的钱
     */
    public function storeUseFee($store_id,$atv_id=''){

        $dis = [

            'a.status'   => 3,

            'a.store_id' => $store_id,
        ];

        if(!empty($atv_id)){

            $dis['a.atv_id'] = $atv_id;
        }

        $cash = $this->alias('a')
                ->join('longbing_card_overlord_atv b','a.atv_id = b.id','left')
                ->where($dis)
                ->where('b.user_id','<>',0)
                ->group('a.id')
                ->sum('a.cash');

        return round($cash,2);


    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-30 15:06
     * @功能说明:更新储值
     */
    public function storedUpdate($record,$pass_user,$uniacid,$type=6){

        $member_model      = new Member();

        $store_order_model = new StoredOrder();

        $member_info  = $member_model->memberUpdateInfo(['uniacid'=>$uniacid,'user_id'=>$record['user_id']]);
        //增加记录
        $res = $store_order_model->desStore($record['cash'],'',$member_info,$type,$pass_user,0,$record['atv_id']);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-03 09:44
     * @功能说明:查看是否有没有审核的活动
     */
    public function recordIng($store_id,$atv_id){

        $dis = [

            'store_id' => $store_id,

            'atv_id'   => $atv_id,

            'status'   => 2
        ];

        $record = $this->where($dis)->find();

        return !empty($record)?1:0;

    }







}